﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.Web.UserControl
{
    public partial class CustomerProductCateTree : UserControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Load += new EventHandler(Page_Load);
        }

        private void BindTree()
        {
            int CateType;//2 代理商-按品牌；1 医院-按功能
            if (base.LoginUser.EntType == (int)EnumHelper.EnterpriseType.Agent)
            { CateType = 1; }
            else { CateType = 2; }
            CustomerBll bll = new CustomerBll();
            List<BasProductCateEntity> dt = bll.GetProductCateListByCustomerID(base.LoginUser.UserID, CateType);

            List<BasProductCateEntity> rows = dt.FindAll(item => item.Grade == 2);

            foreach (BasProductCateEntity row in rows)
            {
                TreeNode node = CreateNode(row.CateName, false, row.CateCode);
                tvProductCate.Nodes.Add(node);

                RecursivelyPopulate(dt, node, row);
            }
            TreeNode nodeSpec = CreateNode("全部", false, "");
            tvProductCate.Nodes.Add(nodeSpec);
        }

        private void RecursivelyPopulate(List<BasProductCateEntity> dt, TreeNode node, BasProductCateEntity current)
        {
            List<BasProductCateEntity> rows = dt.FindAll(item => item.Grade == (current.Grade + 1) && item.CateCode.StartsWith(node.Value));
            foreach (BasProductCateEntity row in rows)
            {
                TreeNode treenode = CreateNode(row.CateName, false, row.CateCode);
                node.ChildNodes.Add(treenode);
                RecursivelyPopulate(dt, treenode, row);
            }
        }

        private TreeNode CreateNode(string text, bool expanded, string id)
        {
            TreeNode node;
            node = new TreeNode(text, id);
            node.Expanded = expanded;
            node.SelectAction = TreeNodeSelectAction.Select;
            if (base.LoginUser.EntType == (int)EnumHelper.EnterpriseType.Agent)
            {
                node.NavigateUrl = ResolveUrl("~/Customer/ProductListByCateForAgent.aspx?CateID=" + id);
            }
            else
            {
                if (id == "") { node.NavigateUrl = ResolveUrl("~/Customer/ProductListByCateForTerminal.aspx" ); }
                else
                {
                    node.NavigateUrl = ResolveUrl("~/Customer/ProductListByCateForTerminal.aspx?CateID=" + id);
                }
            }
            node.Target = "ProductList";
            return node;
        }

        public object SysProductCateEntity { get; set; }
    }
}